﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

//The following NameSpace is to allow use of the FormsAuthentication object
using System.Web.Security;

namespace Authentication
{
    public partial class Site1 : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Context.User.Identity.Name != "")
                {
                    pnlLogin.Visible = false;
                    pnlWelcome.Visible = true;
                    lblWelcome.Text = "Hello " + Context.User.Identity.Name;
                }
                else
                {
                    pnlLogin.Visible = true;
                    pnlWelcome.Visible = false;
                }
            }
        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            if (tbxUserID.Text == "user" && tbxPwd.Text == "pass")
            {
                FormsAuthentication.RedirectFromLoginPage(tbxUserID.Text, false);              
                //assume user's fullname is "Tan Ah kow"
                Session.Add("ewdt_fullname", "Tan Ah kow");
            }
            else
            {
                //Inform user that his userid/password is incorrect.
            }
        }

        protected void btnLogout_Click(object sender, EventArgs e)
        {
            FormsAuthentication.SignOut();
            
            //Clear any session before being redirected
            //Session.Clear();
            Session.Remove("ewdt_fullname");


            Response.Redirect("default.aspx");
        }
    }
}